﻿using System;
using System.Collections.Generic;

namespace SpLocalizer.Common.Extensions
{
    public static class ListExtensions
    {
        /// <summary>
        /// Shuffle List<see cref="T"/> by Fisher–Yates shuffle
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <exception cref="ArgumentNullException"/>
        public static void Shuffle<T>(this IList<T> value)
        {
            int count = value.Count;
            if (count <= 1)
                return;
            var random = new Random();
            while (count > 1)
            {
                int randomIndex = random.Next(count);
                count--;
                T temp = value[randomIndex];
                value[randomIndex] = value[count];
                value[count] = temp;
            }
        }

        /// <summary>
        /// Get random List<see cref="T"/>'s element
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <exception cref="ArgumentNullException"/>
        /// <returns></returns>
        public static T GetRandom<T>(this IList<T> value)
        {
            if (value.Count == 0)
                return default(T);
            return value[new Random().Next(0, value.Count)];
        }
    }
}